package com.yhp.controller;

import com.yhp.bean.Message;
import com.yhp.mvc.ResponseBody;
import com.yhp.mvc.ResponseView;
import com.yhp.service.AdminService;
import com.yhp.service.impl.AdminServiceImpl;
import com.yhp.util.GsonUtil;
import net.sf.json.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Enumeration;

public class AdminController {

    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String adminName = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(adminName);
        System.out.println(password);
        AdminServiceImpl adminService = new AdminServiceImpl();
        boolean b = adminService.loginAdmin(adminName, password);
        System.out.println(b);
        Message msg = null;
        if(b){
            msg = new Message(0,"登录成功");
            Date date = new Date();
            String remoteAddr = request.getRemoteAddr();
            adminService.updateAdminLoginTimeAndLoginIp(adminName,date,remoteAddr);
            request.getSession().setAttribute("adminName",adminName);
        }else{
            msg = new Message(-1,"登录失败");
        }

        String s = GsonUtil.toJSON(msg);
        System.out.println(s);
        return s;
    }


    @ResponseBody("/admin/outLogin.do")
    public String outLogin(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request.getSession().getAttribute("username"));
        request.getSession().invalidate();
        Object username = request.getSession().getAttribute("username");
        System.out.println(username);
        Message msg = new Message();
        if(username==null){
            msg.setStatus(0);
            msg.setResult("退出成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("退出失败");
        }
        String s = GsonUtil.toJSON(msg);

        return s;

    }

}
